<template>
  <div>
      <!-- 卡片框 -->
      <el-card class="box-card">
      <!-- 面包屑导航 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home_Ad' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      </el-breadcrumb>
     
      <!-- 搜索框 -->
        <div class="searchBox">
             <!-- 添加按钮 -->
            <div class="addUserBox" >
                <el-button  type="success" icon="el-icon-circle-plus-outline" @click="isshowAddUserBox=true">添加用户</el-button>
            </div>
            <el-input
            placeholder="请输入内容"
            v-model="query"
            clearable style="width:500px;margin-right:5px">
            </el-input>
            <el-button  type="primary" icon="el-icon-search" @click="search">查找</el-button>
        </div>
          
        <!-- 表格区域 -->
           <el-table
            :data="userList"
            border stripe fit
            empty-text='null'
            class="userTable"
            style="width: 100%;margin-top:12px">
            <el-table-column type="index" label="序号" width="50"></el-table-column>
            <el-table-column prop="username" label="用户名" width="180">
            </el-table-column>
            <el-table-column prop="nickName"  label="昵称" width="180">
            </el-table-column>
            <el-table-column prop="age" label="年龄" width="50">
                   <template slot-scope="row">
                    {{ row.row.age || '暂无' }}
                  </template>
            </el-table-column>
             <el-table-column prop="sex" label="性别" width="50">
                  <template slot-scope="row">
                    {{ row.row.sex || '暂无' }}
                  </template>
            </el-table-column>
             <el-table-column prop="phone" label="电话号码" width="190">
            </el-table-column>
          
            <el-table-column prop="email" label="电子邮箱" width="200">
            </el-table-column>
             <el-table-column  label="操作" width="">
                 <template slot-scope="scope">

                         <el-button type="danger"  icon="el-icon-error" size="medium" @click="removeUser(scope.row.username)">注销账户</el-button>   
                 </template>
            </el-table-column>
            

           </el-table>
         <!-- 添加用户对话框 -->
         <el-dialog width="600px" title="添加用户" :visible.sync="isshowAddUserBox">
             <el-form ref="addUserFormRef"  label-width="80px" :model="addUserForm" :rules="rules">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="addUserForm.username"></el-input>
                </el-form-item>

                <el-form-item label="密码" prop="password">
                    <el-input v-model="addUserForm.password"></el-input>
                </el-form-item>
                 <el-form-item class="formBtnBox"> 
                    <el-button type="primary" @click="add">点击添加</el-button>
                    <el-button  @click="closeAddbox">取消</el-button>
                </el-form-item>
                
             </el-form>
        </el-dialog>
      </el-card>
  </div>
</template>

<script>
import Api from '../../api/index'
export default {
    data() {
        return {
            userList:[],
            addUserForm:{
                username:'',
                password:'',
                
            },
            query:'',
            isshowAddUserBox:false,
            rules:{
                username:[{ required: true, message: '请输入用户名', trigger: 'blur' },
                          { min: 5, max: 12, message: '长度在 5 到 12 个字符', trigger: 'blur'}],
                password:[{ required: true, message: '请输入密码', trigger: 'blur' },
                          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}],
                      
            },
        }
    },
    watch:{
        query(newVal,oldVal){
            if(newVal === '' && oldVal!== ''){
                this.getUserList();
            }
        }
    },
    methods: {
        // 获取用户列表
        async getUserList(){
             const {data:res} = await Api.getUserList();
             console.log(res);
             if(res.status !== 200) return this.$message.error('获取用户数据失败');
             this.userList = res.data;
        },
        async search(){
             const {data:res} = await Api.searchUser(this.query);
             console.log(res);
             if(res.status !== 200) {
                 this.userList = [];
                 return this.$message('无查询结果')
             }
             this.userList = res.data;
        },
        // 删除用户
        removeUser(username){
            console.log(username);
            this.$confirm('此操作将永久注销该用户, 是否继续?', '提示', {
            confirmButtonText: '确定',
            showCancelButton:true,
            cancelButtonText: '取消',
            type: 'warning'
            }).then(async() => {
              const {data:res} = await Api.removeUser(username);
              if(res.status !== 200 ) return this.$message.error('注销失败');
              this.$message.success('注销成功');
              this.getUserList();
            }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消删除'
            });          
            });
        },
        // 关闭添加用户对话框
        closeAddbox(){
            this.isshowAddUserBox = false;
            this.$refs.addUserFormRef.resetFields();
        },
        // 添加用户
        add(){
            // 验证表单
             this.$refs.addUserFormRef.validate(async valid=>{
                 if(!valid) return;
                 const {data:res} = await Api.addUser(this.addUserForm);
                 if(res.status !== 201 ) return this.$message.error('添加失败');
                 this.$message.success('用户添加成功');
                 this.getUserList();
                 this.isshowAddUserBox=false;
             })
        }
            
    },
    created() {
        this.getUserList();
    },
}
</script>

<style scoped lang="less">
    .searchBox{
        margin-top: 20px;
        display: flex;
        
    }
    .addUserBox{
        margin-right: 50px;
    }
    .formBtnBox{
        display: flex;
        justify-content: flex-end;
    }
 
</style>